"use strict";

(function () {
  function r(e, n, t) {
    function o(i, f) {
      if (!n[i]) {
        if (!e[i]) {
          var c = "function" == typeof require && require;
          if (!f && c) return c(i, !0);
          if (u) return u(i, !0);
          var a = new Error("Cannot find module '" + i + "'");
          throw a.code = "MODULE_NOT_FOUND", a;
        }

        var p = n[i] = {
          exports: {}
        };
        e[i][0].call(p.exports, function (r) {
          var n = e[i][1][r];
          return o(n || r);
        }, p, p.exports, r, e, n, t);
      }

      return n[i].exports;
    }

    for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) {
      o(t[i]);
    }

    return o;
  }

  return r;
})()({
  1: [function (require, module, exports) {
    "use strict"; // const { data } = require("autoprefixer");

    loader.define({
      depend: ['pages/config/config'],
      // beforeLoad: function(config){
      //     var script = document.createElement("script");  
      //     script.src = "http://api.map.baidu.com/api?v=2.0&ak=LniWrEprri0pDMBzhFpInn19B6TDPed5";
      //     document.body.appendChild(script); 
      // },
      loaded: function loaded(config, require, exports, module, global) {
        this.pageview = {};

        this.pageview.init = function () {
          // window.onload = config.loadScript();
          // 这里到时候可以改成 search-item 的 bui-btn点击时，获取它的id，从而对应数据库取数据
          // 百度地图api
          // Geolocation() 创建Geolocation对象 getCurrentPosition() 函数获取位置 返回对象 res.address.city 获取当前城市名
          // Map() 创建map对象 getDistance(start: Point, end: Point) 根据两点经纬度，计算两地的距离，获取实际距离
          // demo中有相应的代码示例
          // console.log(config.apiurl);
          // console.log('lll');
          var map = new BMap.Map();
          var geolocation = new BMap.Geolocation();
          geolocation.getCurrentPosition(function (r) {
            var city, plng, plat;

            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
              // 取到城市
              console.log(r.address);
              console.log(r.point);
              city = r.address.city;
              console.log(r.longitude, ",", r.latitude); // 取到经纬度

              navigator.geolocation.getCurrentPosition(function (position) {
                //得到html5定位结果
                plng = position.coords.longitude;
                plat = position.coords.latitude;
                console.log(plng + ',' + plat);
                var currentp = new BMap.Point(plng, plat);
                console.log(city);
                var data_ = {
                  // 这里是获取的city
                  city: city
                };
                data_ = JSON.stringify(data_);
                bui.ajax({
                  // url: 相对路径
                  url: config.apiurl + "api/v1.0/searchact",
                  data: data_,
                  method: "post",
                  headers: {
                    'Content-Type': 'application/json'
                  }
                }).then(function (res) {
                  console.log(res); // console.log(res['activities']);
                  // console.log(typeof(res['activities']))
                  // 字典
                  // {type名称: [{id活动: id, 比较参数: 热度系数hot - 距离系数, name: 活动名称, distance: 距离, pageview: 浏览量}, ...]}

                  var msg_type = ['游戏/娱乐', '学习/交流', '户外/休闲', '运动/健身', '自定义'];
                  var msg = {
                    "游戏/娱乐": {
                      'subname': 'entertainment',
                      'data': []
                    },
                    "学习/交流": {
                      'subname': 'study',
                      'data': []
                    },
                    "户外/休闲": {
                      'subname': 'outdoor',
                      'data': []
                    },
                    "运动/健身": {
                      'subname': 'sports',
                      'data': []
                    },
                    "自定义": {
                      'subname': 'userdefined',
                      'data': []
                    }
                  };
                  var activity = res['activities'];

                  for (var i in activity) {
                    var type = activity[i]["type"];
                    var lng = activity[i]['longitude'];
                    var lat = activity[i]['latitude'];
                    var destp = new BMap.Point(lng, lat);
                    var dis = map.getDistance(destp, currentp);
                    dis = dis / 1000;
                    var detail = {
                      "id": activity[i]["id"],
                      "name": activity[i]["name"],
                      "pageview": activity[i]["pageview"],
                      "distance": dis.toFixed(2),
                      "cmp": activity[i]["hot"] - dis,
                      "lng": lng,
                      "lat": lat,
                      "type": type
                    };
                    msg[type]['data'].push(detail);
                  }

                  function rule(a, b) {
                    return b['cmp'] - a['cmp'];
                  }

                  for (var i in msg) {
                    var tmsg = msg[i]['data']; // console.log(tmsg);

                    tmsg.sort(rule);
                    console.log(tmsg);
                    var count = 0;

                    for (var j in tmsg) {
                      count = count + 1;
                      var searchItem = $('<li class="search-item" style="width: 2.5rem;"></li>');
                      var btn = $('<div class="bui-btn item-btn"></div>');
                      btn.attr("id", tmsg[j]['id']);
                      var btnPic = $('<div class="bui-pic"></div>');
                      var itemName = $('<div class="bui-box item-name"></div>');
                      itemName.text(tmsg[j]['name']);
                      btnPic.append(itemName); // itemDistance

                      var itemDistance = $('<div class="bui-box-align-middle item-detail"></div>');
                      itemDistance.append($('<img src="images/search/place_16.png">'));
                      itemDistance.append($('<div>' + tmsg[j]['distance'] + 'km</div>'));
                      btnPic.append(itemDistance); // itemView

                      var itemView = $('<div class="bui-box-align-middle item-detail"></div>');
                      itemView.append($('<img src="images/search/look_16.png">'));
                      itemView.append($('<div>' + tmsg[j]['pageview'] + '</div>'));
                      btnPic.append(itemView); // itemLng

                      var itemLng = $('<p></p>');
                      itemLng.attr('id', 'lng' + tmsg[j]['id']);
                      itemLng.text(tmsg[j]['lng']);
                      itemLng.hide();
                      btnPic.append(itemLng); // itemLat

                      var itemLat = $('<p></p>');
                      itemLat.attr('id', 'lat' + tmsg[j]['id']);
                      itemLat.text(tmsg[j]['lat']);
                      itemLat.hide();
                      btnPic.append(itemLat); // itemType

                      var itemType = $('<p></p>');
                      itemType.attr('id', 'type' + tmsg[j]['id']);
                      itemType.text(tmsg[j]['type']);
                      itemType.hide();
                      btnPic.append(itemType);
                      btn.append(btnPic);
                      searchItem.append(btn);
                      $('#' + msg[i]['subname']).append(searchItem);
                      if (count == 10) break;
                    }

                    var more = $('<li class="search-item" style="width: 2.5rem;"><div class="bui-btn more"><div class="bui-pic bui-box-center"><span class="more-content">点击查看更多</span><span class="tag" style="display:none">' + i + '</span></div></div></li>');
                    $('#' + msg[i]['subname']).append(more);
                  }

                  $(".more").on("click", function (e) {
                    var tag = $(this).find('.tag');
                    console.log(tag);
                    tag = tag[0];
                    console.log($(tag).text());
                    bui.load({
                      url: 'pages/searchmore/searchmore',
                      param: {
                        msg: msg[$(tag).text()]['data'],
                        type: $(tag).text()
                      }
                    });
                  });
                  $(".item-btn").on("click", function (e) {
                    // 将活动id作为唯一标识，传给detail界面
                    // alert("hhh");
                    // alert($(this).attr('id'));
                    var id = $(this).attr('id');
                    var lng = $('#lng' + id).text();
                    var lat = $('#lat' + id).text();
                    var type = $('#type' + id).text(); // console.log(lat);
                    // console.log(type);

                    bui.load({
                      url: "pages/activitydetail/activitydetail",
                      param: {
                        id: id,
                        type: type,
                        lng: lng,
                        lat: lat
                      }
                    });
                  });
                }, function (res, status) {
                  console.log(status);
                });
              }, function (err) {
                console.log(err.code);
              }, {
                enableHighAcuracy: true
              });
            } else {
              alert('failed' + this.getStatus());
            }
          }, {
            enableHighAccuracy: true
          }); // console.log(currentp);
          // console.log(city);
        };

        this.pageview.init();

        this.pageview.refresh = function () {
          location.reload();
        };

        return this.pageview;
      },
      show: function show(e) {
        if (e.type == 'load' || e.type == 'back') this.pageview.refresh();
      }
    });
  }, {}]
}, {}, [1]);